import { localStorage } from 'mp-storage'
import config from '@/config'

const KeyPrefix = `zhdj-${config.env}-` // 键前缀
// 缓存的键
const localStorageKeys = [
  'homeApiResult',
  'Authorization', // 用户标识
  'orgTree', // 组织树结构
  'communityTree', // 社区树结构
  'mpImgs',
  'homeNav'
] // 保存在localStorage中的键
const keys = [...localStorageKeys]

// 操作
const operation = {
  get () {
    const key = `${KeyPrefix}${this.key}`
    let value = localStorage.getItem(key)
    try {
      value = JSON.parse(value)
    } catch (e) {
      // console.log();
    }
    return value
  },
  set (value) {
    const key = `${KeyPrefix}${this.key}`
    if (typeof value === 'object') {
      value = JSON.stringify(value)
    }
    localStorage.setItem(key, value)
  },
  remove () {
    const key = `${KeyPrefix}${this.key}`
    localStorage.removeItem(key)
  },
}

const Cache = {}

keys.forEach(key => {
  Cache[key] = {
    key,
    ...operation,
  }
})

export default Cache
